package com.icss.daos;
/*
 * 论坛主版块dao类
 * 管志春
 * 2010.03.31
 * 
 * */
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.icss.beans.ClassVO;
import com.icss.utils.DB;

public class ClassDao {

	private Connection conn = null;

	private Statement stat = null;

	private ResultSet rs = null;
	
	//获取所有版块信息
	public ArrayList<ClassVO> getAllClass() {

		ArrayList<ClassVO> classvos = new ArrayList<ClassVO>();

		conn = DB.getConnection();
		String sql = "select * from Class";

		try {
			stat = conn.createStatement();
			rs = stat.executeQuery(sql);

			while (rs.next()) {
				ClassVO classVO = new ClassVO();
				classVO.setClassID(rs.getInt("classID"));
				classVO.setClassName(rs.getString("className"));
				classvos.add(classVO);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		close();
		return classvos;
	}
	
	//根据id获取版块信息
	public ClassVO queryClassByID(int id) {

		ClassVO classvo = new ClassVO();

		String queryStr = "select * from Class where id =" + id;

		try {
			stat = conn.createStatement();
			rs = stat.executeQuery(queryStr);

			while (rs.next()) {
				classvo.setClassID(rs.getInt("classID"));
				classvo.setClassName(rs.getString("className"));

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		close();
		return classvo;

	}
	
	//执行SQL语句
	public boolean executeSQL(String sql) {

		int result = 0;
		try {
			conn = DB.getConnection();
			stat = conn.createStatement();
			result = stat.executeUpdate(sql);
		} catch (SQLException e) {
			close();
			e.printStackTrace();
		}

		close();

		if (result != 0) {
			return true;
		}
		return false;

	}

	public void close() {

		try {

			if (null != conn) {
				conn.close();
			}

			if (null != stat) {
				stat.close();
			}

			if (null != rs) {
				rs.close();
			}

		} catch (SQLException e) {
			e.printStackTrace();

		}

	}

}
